//
//  ViewController.m
//  NSUserDefaultsDemo
//
//  Created by UniHan_iMac on 17/3/31.
//  Copyright © 2017年 UniHan_iMac. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    
    // 存数据
    [userDefaults setInteger:18 forKey:@"int"];
    [userDefaults setBool:YES forKey:@"bool"];
    [userDefaults setObject:@"字符串" forKey:@"string"];
    [userDefaults setObject:@[@"123"] forKey:@"array"];
    [userDefaults setObject:@{@"key":@"value"} forKey:@"dictionary"];
    [userDefaults setObject:[@"字符串转二进制数据" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"data"];
    
    // 强制写入
    [userDefaults synchronize];
    
    // 读数据
    NSLog(@"%ld",(long)[userDefaults integerForKey:@"int"]);
    NSLog(@"%d",[userDefaults boolForKey:@"bool"]);
    NSLog(@"%@",[userDefaults objectForKey:@"string"]);
    NSLog(@"%@",[userDefaults objectForKey:@"array"]);
    NSLog(@"%@",[userDefaults objectForKey:@"dictionary"]);
    // 二进制数据转字符串
    NSData *data = [userDefaults objectForKey:@"data"];
    NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}

@end
